﻿using System;
using System.Data;
using System.IO;
using System.Text;

namespace PLM.ConfigurationCenter.Backup.Handle {
    public class TextBoxWriter {
        RichTextBox rtBox;
        public delegate void VoidAction();

        public TextBoxWriter(RichTextBox box) {
            Control.CheckForIllegalCrossThreadCalls = false;
            rtBox = box;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void WriteLine(string value) {
            VoidAction action = delegate {
                try {
                    //保存信息到文件夹
                    //DbLogger.LogWrite(value);
                    string[] strLines = rtBox.Text.Split('\n');
                    if (strLines.Length > 1000) {
                        rtBox.Clear();
                    }
                    if (!string.IsNullOrEmpty(value)) {
                        //让文本框获取焦点  
                        rtBox.Focus();
                        //设置光标的位置到文本尾  
                        rtBox.Select(rtBox.TextLength, 0);
                        //滚动到控件光标处  
                        rtBox.ScrollToCaret();
                        rtBox.AppendText(string.Format("\r\n[{0:HH:mm:ss}]{1}\r\n", DateTime.Now, value));
                        //更改颜色
                        //rtBox.ForeColor = Color.FromArgb(51, 255, 102);
                    } else {
                        rtBox.AppendText("暂无日志");
                    }


                } catch (Exception ex) {
                    //DbLogger.LogException(ex, ex.Message);
                }

            };
            if (rtBox.IsHandleCreated) {
                try {
                    rtBox.BeginInvoke(action);
                } catch { }
            }

        }
    }

}
